using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel;
using NHBussines;

namespace NHServices.Core
{
    public abstract class GenericService<T,Id> : IGenericService<T,Id>
    {
        private BussinesFactory bussinesFactory = BussinesFactory.Instance(BussinesFactory.BUSSINESS_FACTORY);

        public BussinesFactory BussinesFactory
        {
            get { return bussinesFactory; }
            set { bussinesFactory = value; }
        }

        public IGenericController <T, Id> genericService { set; get; }


        public T FindByid(Id id)
        {
            return genericService.FindByid(id);
        }

        public IList<T> FindAll()
        {
            return genericService.FindAll();
        }

        public IList<T> FindAllOrdered(IList<string> propertyNames)
        {
            return genericService.FindAllOrdered(propertyNames);
        }

        public IList<T> FindByExample(T entity)
        {
            return genericService.FindByExample(entity);
        }

        public IList<T> FindBydExampleOrdered(T entity, IList<string> propertyNames)
        {
            return genericService.FindBydExampleOrdered(entity, propertyNames);
        }

        public IList<T> FindLikeExample(T entity)
        {
            return genericService.FindLikeExample(entity);
        }

        public IList<T> FindLikeExampleOrdered(T entity, IList<string> propertyNames)
        {
            return genericService.FindLikeExampleOrdered(entity, propertyNames);
        }

        public IList<T> FindLikeExampleIgnoreCase(T entity)
        {
            return genericService.FindLikeExampleIgnoreCase(entity);
        }

        public IList<T> FindLikeExampleIgnoreCaseOrdered(T entity, IList<string> propertyNames)
        {
            return genericService.FindLikeExampleIgnoreCaseOrdered(entity, propertyNames);
        }

        public IList<T> FindByQuery(string query)
        {
            return genericService.FindByQuery(query);
        }

        public IList<T> FindByQueryWithParameters(string query, string[] parameters, object[] values)
        {
            return genericService.FindByQuery(query, parameters, values);
        }

        public IList<T> FindByNamedQuery(string query)
        {
            return genericService.FindByNamedQuery(query);
        }

        public IList<T> FindByNamedQueryWithParameters(string query, string[] parameters, object[] values)
        {
            return genericService.FindByNamedQuery(query, parameters, values);
        }

        public IList FindObjectsByQuery(string query)
        {
            return genericService.FindObjectsByQuery(query);
        }

        public IList FindObjectsByNamedQuery(string query)
        {
            return genericService.FindObjectsByNamedQuery(query);
        }

        public IList FindObjectsByNamedQueryWithParameters(string query, string[] parameteres, object[] values)
        {
            return genericService.FindObjectsByNamedQuery(query, parameteres, values);
        }

        public IList FindObjectsBySQLQuery(string query, string[] parameters, object[] values)
        {
            return genericService.FindObjectsBySQLQuery(query, parameters, values);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public T Save(T entity)
        {
            return genericService.Save(entity);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public T Update(T entity)
        {
            return genericService.Update(entity);
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public int DeleteById(Id id)
        {
            try
            {
                genericService.DeleteById(id);
                return 1;
            }
            catch(Exception exception)
            {
                return 0;
            }
            
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public int Delete(T entity)
        {
            try
            {
                genericService.Delete(entity);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public Id GetMax(string propertyName)
        {
            return genericService.GetMax(propertyName);
        }
   
    }
}